package Encoder;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

public class MainFrame extends JFrame{
	public JTextArea uncodedText;
	public JTextArea codedText;
	public JButton encodeButton;
	public JButton decodeButton;
	public JButton changeSeedButton;
	public JButton copyUp;
	public JButton copyDown;
	public GridLayout gl;
	public String inputText = "";
	public JPanel buttonPanel;
	public Coder coder;
	
	String hardSeed = "muahaha";
	
	public ActionListener actionListener = new ActionListener() {
		@Override
		public void actionPerformed(ActionEvent e) {
			// TODO Auto-generated method stub
			if(e.getActionCommand().equals("encode")){
				codedText.setText(coder.encode(uncodedText.getText(), coder.encode(coder.getPassword(), hardSeed)));
			}
			if(e.getActionCommand().equals("decode")){
				codedText.setText(coder.decode(uncodedText.getText(), coder.encode(coder.getPassword(), hardSeed)));
			}
			if(e.getActionCommand().equals("changeSeed")){
				String pw = JOptionPane.showInputDialog(new JFrame(), "Set Password", "Set Password", JOptionPane.OK_OPTION);
				if(pw != null && !pw.equals("")){
					coder.setPassword(pw);
				} else {
					coder.setPassword(hardSeed);
				}
			}
			if(e.getActionCommand().equals("copyDown")){
				codedText.setText(uncodedText.getText());
			}
			if(e.getActionCommand().equals("copyUp")){
				uncodedText.setText(codedText.getText());			}
		}
	};
	
	public MainFrame() {
		
		setLayout(null);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setSize(800, 600);
		setLocation(300, 50);
		setResizable(false);
		setTitle("Encoder");
		
		coder = new Coder();
		coder.setPassword(JOptionPane.showInputDialog(new JFrame(), "Set Password", "Set Password", JOptionPane.OK_OPTION));
		
		buttonPanel = new JPanel();
		buttonPanel.setSize(790, 40);
		buttonPanel.setLocation(0, 280);
		encodeButton = new JButton("Encode");
		decodeButton = new JButton("Decode");
		changeSeedButton = new JButton("Change Password");
		copyUp = new JButton("Copy Up");
		copyDown = new JButton("Copy Down");
		encodeButton.addActionListener(actionListener); encodeButton.setActionCommand("encode");
		decodeButton.addActionListener(actionListener); decodeButton.setActionCommand("decode");
		changeSeedButton.addActionListener(actionListener); changeSeedButton.setActionCommand("changeSeed");
		copyUp.addActionListener(actionListener); copyUp.setActionCommand("copyUp");
		copyDown.addActionListener(actionListener); copyDown.setActionCommand("copyDown");
		
		JScrollPane uncodedPane = new JScrollPane();
		uncodedPane.setWheelScrollingEnabled(true);
		uncodedText = new JTextArea();
		uncodedText.setText("");
		uncodedPane.setSize(790, 280);
		uncodedText.setSize(1200, 280);
		uncodedPane.setLocation(0, 0);
		uncodedText.setLineWrap(true);
		uncodedText.setWrapStyleWord(true);
		
		JScrollPane codedPane = new JScrollPane();
		codedPane.setWheelScrollingEnabled(true);
		codedText = new JTextArea();
		codedText.setText("");
		codedPane.setSize(790, 280);
		codedText.setSize(1200, 280);
		codedPane.setLocation(0, 320);
		codedText.setLineWrap(true);
		codedText.setWrapStyleWord(true);
		
		codedPane.add(codedText);
		uncodedPane.add(uncodedText);
		
		add(uncodedPane);
		add(buttonPanel);
		buttonPanel.add(encodeButton);
		buttonPanel.add(decodeButton);
		buttonPanel.add(changeSeedButton);
		buttonPanel.add(copyUp);
		//buttonPanel.add(copyDown);
		add(codedPane);
		setVisible(true);
	}
}
